/*************************************************************************
 *
 *  Copyright (C) 2013 Florian Dang <florian.coin@gmail.com>
 *
 *  This file is part of NatusVerse.
 *
 *  NatusVerse is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  NatusVerse is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NatusVerse.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

#ifndef INC_GRAPHICS_COLOR_H_
#define INC_GRAPHICS_COLOR_H_

/*!
 * \file        color.h
 * \brief       Color file management
 * \author      Florian Dang
 * \date        2013
 */

#include <stdio.h>
#include <stdint.h>
#include <cmath>
#include <algorithm>

namespace color
{
typedef uint32_t t_color;
typedef unsigned int uint;
class Color
{
	public:
		static const t_color max_color = 256;
		Color() {}
		Color(t_color color0)
			: color_(color0), 
			  r_((color0 >> 16) & (max_color-1)),
			  g_((color0 >> 8) & (max_color-1)),
			  b_(color0 & (max_color-1)) {}

		Color(uint r0, uint g0, uint b0)
			: color_(((r0 % max_color) << 16) + ((g0 % max_color) << 8) + (b0 % max_color)), 
			r_(r0), g_(g0), b_(b0){}

		~Color() {}

		inline t_color color() const { return color_; }
		inline uint r() const { return r_; }
		inline uint g() const { return g_; }
		inline uint b() const { return b_; }

		inline void SetColor()
		{
			color_ = ((r() % max_color) << 16) + ((g() % max_color) << 8) + (b() % max_color);
		}

		inline void set_color(t_color color0) 
		{
			color_ = color0;
			r_ = ((color0 >> 16) & (max_color-1));
		  	b_ = ((color0 >> 8) & (max_color-1));
		  	g_ = (color0 & (max_color-1));	
		}

		inline void set_color(uint r0, uint g0, uint b0)
		{ 
			color_ = (((r0 % max_color) << 16) + ((g0 % max_color) << 8) + (b0 % max_color));
			r_ = r0;
			g_ = g0;
			b_ = b0;
		}

		static t_color ColorRGB(uint r, uint g, uint b)
		{
			return ((r % max_color) << 16) + ((g % max_color) << 8) + (b % max_color);
		}

		inline uint color_r() const { return (color() >> 16) & (max_color-1); }
		inline uint color_g() const { return (color() >> 8) & (max_color-1); }
		inline uint color_b() const { return (color()) & (max_color-1); }

		void set_r(uint r0) { r_ = r0; }
		void set_g(uint g0) { g_ = g0; }
		void set_b(uint b0) { b_ = b0; }

		void CalibrateMin()
		{
			set_r(std::min(r(), max_color-1));
			set_g(std::min(g(), max_color-1));
			set_b(std::min(b(), max_color-1));
		}

		void Print() const
		{
			if (color_r() != r())
				printf("(!) red is not the same\n");
			if (color_g() != g())
				printf("(!) green is not the same\n");
			if (color_b() != b())
				printf("(!) blue is not the same\n");
			printf("#%d %d %d#", color_r(), color_g(), color_b());
		}

	private:
		t_color color_;
		uint r_;
		uint g_;
		uint b_;
};


const t_color aliceblue       = 0xF0F8FF;
const t_color antiquewhite    = 0xFAEBD7;
const t_color aqua            = 0x00FFFF;
const t_color aquamarine      = 0x7FFFD4;
const t_color azure           = 0xF0FFFF;
const t_color beige           = 0xF5F5DC;
const t_color bisque          = 0xFFE4C4;
const t_color black           = 0x000000;
const t_color blanchedalmond  = 0xFFEBCD;
const t_color blue            = 0x0000FF;
const t_color blueviolet      = 0x8A2BE2;
const t_color brown           = 0xA52A2A;
const t_color burlywood       = 0xDEB887;
const t_color cadetblue       = 0x5F9EA0;
const t_color chartreuse      = 0x7FFF00;
const t_color chocolate       = 0xD2691E;
const t_color coral           = 0xFF7F50;
const t_color cornflowerblue  = 0x6495ED;
const t_color cornsilk        = 0xFFF8DC;
const t_color crimson         = 0xDC143C;
const t_color cyan            = 0x00FFFF;
const t_color darkblue        = 0x00008B;
const t_color darkcyan        = 0x008B8B;
const t_color darkgoldenrod   = 0xB8860B;
const t_color darkgray        = 0xA9A9A9;
const t_color darkgreen       = 0x006400;
const t_color darkkhaki       = 0xBDB76B;
const t_color darkmagenta     = 0x8B008B;
const t_color darkolivegreen  = 0x556B2F;
const t_color darkorange      = 0xFF8C00;
const t_color darkorchid      = 0x9932CC;
const t_color darkred         = 0x8B0000;
const t_color darksalmon      = 0xE9967A;
const t_color darkseagreen    = 0x8FBC8F;
const t_color darkslateblue   = 0x483D8B;
const t_color darkslategray   = 0x2F4F4F;
const t_color darkturquoise   = 0x00CED1;
const t_color darkviolet      = 0x9400D3;
const t_color deeppink        = 0xFF1493;
const t_color deepskyblue     = 0x00BFFF;
const t_color dimgray         = 0x696969;
const t_color dodgerblue      = 0x1E90FF;
const t_color firebrick       = 0xB22222;
const t_color floralwhite     = 0xFFFAF0;
const t_color forestgreen     = 0x228B22;
const t_color fuchsia         = 0xFF00FF;
const t_color gainsboro       = 0xDCDCDC;
const t_color ghostwhite      = 0xF8F8FF;
const t_color gold            = 0xFFD700;
const t_color goldenrod       = 0xDAA520;
const t_color gray            = 0x808080;
const t_color green           = 0x008000;
const t_color greenyellow     = 0xADFF2F;
const t_color honeydew        = 0xF0FFF0;
const t_color hotpink         = 0xFF69B4;
const t_color indianred       = 0xCD5C5C;
const t_color indigo          = 0x4B0082;
const t_color ivory           = 0xFFFFF0;
const t_color khaki           = 0xF0E68C;
const t_color lavender        = 0xE6E6FA;
const t_color lavenderblush   = 0xFFF0F5;
const t_color lawngreen       = 0x7CFC00;
const t_color lemonchiffon    = 0xFFFACD;
const t_color lightblue       = 0xADD8E6;
const t_color lightcoral      = 0xF08080;
const t_color lightcyan       = 0xE0FFFF;
const t_color lightgoldenrodyellow = 0xFAFAD2;
const t_color lightgreen      = 0x90EE90;
const t_color lightgrey       = 0xD3D3D3;
const t_color lightpink       = 0xFFB6C1;
const t_color lightsalmon     = 0xFFA07A;
const t_color lightseagreen   = 0x20B2AA;
const t_color lightskyblue    = 0x87CEFA;
const t_color lightslategray  = 0x778899;
const t_color lightsteelblue  = 0xB0C4DE;
const t_color lightyellow     = 0xFFFFE0;
const t_color lime            = 0x00FF00;
const t_color limegreen       = 0x32CD32;
const t_color linen           = 0xFAF0E6;
const t_color magenta         = 0xFF00FF;
const t_color maroon          = 0x800000;
const t_color mediumaquamarine= 0x66CDAA;
const t_color mediumblue      = 0x0000CD;
const t_color mediumorchid    = 0xBA55D3;
const t_color mediumpurple    = 0x9370DB;
const t_color mediumseagreen  = 0x3CB371;
const t_color mediumslateblue = 0x7B68EE;
const t_color mediumspringgreen=0x00FA9A;
const t_color mediumturquoise = 0x48D1CC;
const t_color mediumvioletred = 0xC71585;
const t_color midnightblue    = 0x191970;
const t_color mintcream       = 0xF5FFFA;
const t_color mistyrose       = 0xFFE4E1;
const t_color moccasin        = 0xFFE4B5;
const t_color navajowhite     = 0xFFDEAD;
const t_color navy            = 0x000080;
const t_color oldlace         = 0xFDF5E6;
const t_color olive           = 0x808000;
const t_color olivedrab       = 0x6B8E23;
const t_color orange          = 0xFFA500;
const t_color orangered       = 0xFF4500;
const t_color orchid          = 0xDA70D6;
const t_color palegoldenrod   = 0xEEE8AA;
const t_color palegreen       = 0x98FB98;
const t_color paleturquoise   = 0xAFEEEE;
const t_color palevioletred   = 0xDB7093;
const t_color papayawhip      = 0xFFEFD5;
const t_color peachpuff       = 0xFFDAB9;
const t_color peru            = 0xCD853F;
const t_color pink            = 0xFFC0CB;
const t_color plum            = 0xDDA0DD;
const t_color powderblue      = 0xB0E0E6;
const t_color purple          = 0x800080;
const t_color red             = 0xFF0000;
const t_color rosybrown       = 0xBC8F8F;
const t_color royalblue       = 0x4169E1;
const t_color saddlebrown     = 0x8B4513;
const t_color salmon          = 0xFA8072;
const t_color sandybrown      = 0xF4A460;
const t_color seagreen        = 0x2E8B57;
const t_color seashell        = 0xFFF5EE;
const t_color sienna          = 0xA0522D;
const t_color silver          = 0xC0C0C0;
const t_color skyblue         = 0x87CEEB;
const t_color slateblue       = 0x6A5ACD;
const t_color slategray       = 0x708090;
const t_color snow            = 0xFFFAFA;
const t_color springgreen     = 0x00FF7F;
const t_color steelblue       = 0x4682B4;
const t_color tancolor        = 0xD2B48C;
const t_color teal            = 0x008080;
const t_color thistle         = 0xD8BFD8;
const t_color tomato          = 0xFF6347;
const t_color turquoise       = 0x40E0D0;
const t_color violetlight     = 0xEE82EE;
const t_color wheat           = 0xF5DEB3;
const t_color white           = 0xFFFFFF;
const t_color whitesmoke      = 0xF5F5F5;
const t_color yellow          = 0xFFFF00;
const t_color yellowgreen     = 0x9ACD32;

}
#endif  // INC_GRAPHICS_COLOR_H_
